package com.github.netty.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author ArchieSean
 * @description 用于获取ioc容器，提供对外使用方法
 * @date 2024-08-14
 */
@Component
public class NettyApplicationContext  implements ApplicationContextAware {

    /**
     * ico容器
     */
    private  static  ApplicationContext APPLICATION_CONTEXT ;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        NettyApplicationContext.APPLICATION_CONTEXT=applicationContext;
    }


    /**
     * 获取spring容器
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }


    /**
     * 从容器中获取指定bean
     *
     * @param clazz 字节码
     * @param <T>   泛型
     * @return obj
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

}
